import axios from 'axios'
import { use } from 'element-ui/src/locale'
import Qs from 'qs'

// 全局默认配置
axios.defaults.baseURL = 'http://www.childrenwear.com:88/api/childrenwear_product'

axios.defaults.withCredentials = true // 请求发送cookie

// axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8'

// 获取验证码
export const sendSms = (mobile) => {
  return axios.get('/sms/sendSms?mobile=' + mobile)
}

// 手机号验证码注册
export const register = (mobile, code) => {
  return axios.post('/user/register?mobile=' + mobile + '&code=' + code)
}

// 手机号验证码登录
export const login = (mobile, code) => {
  return axios.post('/user/login?mobile=' + mobile + '&code=' + code)
}

// 加载页面时获取用户是否登录（若登录了就返回用户信息，没登录就说明也不做）
export const getUser = () => {
  return axios.post('/auth/getUser')
}

// 用户名密码登录
export const userNameLogin = (username, password) => {
  return axios.post('/user/userNameLogin?username=' + username + '&password=' + password)
}

// 用户名密码登录
export const weiboLogin = () => {
  return axios.get('https://api.weibo.com/oauth2/authorize?client_id=839991271&response_type=code&redirect_uri=http://localhost:88/api/childrenwear_product/oauth2.0/weibo/success')
}

// 用户登出
export const logout = () => {
  return axios.get('/product/userbase/logout')
}

// 修改密码
export const submitChangePassword = (uid, oldPwd, newPwd) => {
  return axios.post('/product/userbase/changePassword?uid=' + uid + '&oldPwd=' + oldPwd + '&newPwd=' + newPwd)
}

// 用户信息更新
export const userUpdate = (user) => {
  return axios.post('/product/userbase/update', user)
}

//用户头像更新
export const userAvatarUpdate = (params) => {
  return axios.post('/oss/fileOss',params)
}

// 微博端授权同意后，发送后端请求登录，若该用户没有通过这种方式注册过，就先注册再返回用户信息，否则直接返回用户信息
export const oauthCallbackPost = (code) => {
  return axios.post('/oauth2.0/weibo/success?code=' + code)
}

// 获取到品牌列表数据
export const getBrands = (params) => {
  return axios.post('/product/brandinfo/list', Qs.stringify(params))
}

// 获取热销商品列表
export const getHots = (params) => {
  return axios.post('/product/productinfo/getHosts', Qs.stringify(params))
}

// 获取所有的热销商品列表
export const getAllHots = () => {
  return axios.post('/product/productinfo/getAllHots')
}

// 获取商品的详情
export const getProductById = (productId) => {
  return axios.post('/product/productinfo/info/' + productId)
}

// 添加到购物车
export const addToCart = (form) => {
  return axios.post('/product/cart/addToCart', Qs.stringify(form))
}

// 获取到店铺列表数据
export const getShops = (params) => {
  return axios.post('/product/supplierinfo/list', Qs.stringify(params))
}

// 获取活动以及品牌新闻
export const getEvents = () => {
  return axios.get('/product/event/getEvents')
}

// 根据活动id获取活动
export const getEventById = (eventId) => {
  return axios.get('/product/event/info/' + eventId)
}

// 获取所有店铺注册好的地区列表
export const getALlUseRegions = () => {
  return axios.get('/product/supplierinfo/getAllUseRegions')
}
